var consts = require('../../../util/consts');
var random = require("string-random");
var oilLog = require('../../../domain/oilLog');
var pushUtils = require('../../../util/pushUtils');
var utils = require('../../../util/utils');
var async = require('async');
var AsyncLock = require('async-lock')
const {formatCoins} = require("../../upgrade/lib/action");
const BigNumber = require("bignumber.js");

var lock = new AsyncLock({timeout: 1000});
module.exports = function (app) {
    return new Handler(app);
};
var Handler = function (app) {
    this.app = app;
};

//生成随机金币数量，2020年7月23日，增加倍数为500-1000倍，原先为0-10K，修改为500-10M
function randomGold() {
    let minNum = 500;   //金币最少为500
    let maxNum = 10000000;    //金币最大为10M
    let value = parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10)
    //格式化后的金币
    let showVal = formatCoins(new BigNumber(value));
    return {value, showVal};
}

//生成奖品列表
let createPrizeList = (total_oil) => {
    //奖品
    let welfare = consts.waelfare;
    let list = [];
    for (let i = 0; i < welfare.length; i++) {
        let prize = {};
        //当单日石油小于15时可以产生石油
        if (total_oil < 15) {
            prize.type = consts.MoneyType.OIL;//奖品类型
            let arr = [1, 2, 3];
            let index = Math.floor((Math.random() * arr.length));
            prize.value = arr[index];
            if (prize.value < 1) {
                prize.value = 1;
            }
            prize.showVal = prize.value;
        } else {
            let {value, showVal} = randomGold();
            prize.type = consts.MoneyType.GOLD;
            prize.value = value;
            prize.showVal = showVal;
        }
        prize.id = i;
        list.push(prize);
    }
    return list;
}

var handler = Handler.prototype;
/**
 * 福利时刻用户抽奖
 * @param msg
 * @param session
 * @param next
 */
handler.welfare = function (msg, session, next) {
    var uid = session.uid;
    if (!uid) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.MISSING_PARAMETER
        })
    }
    var self = this;
    self.app.rpc.login.loginRemote.getUser(session, uid, function (err, user) {
        if (err) {
            next(err, {
                code: consts.Code.ERR,
                msg: err
            })
            return;
        }

        //返回的奖品
        let prizeList = createPrizeList(user.welfare_oil);
        self.app.userRemote(uid, "set", [uid, "user.welfare", prizeList]);
        next(null, {
            code: consts.Code.SUC,
            msg: consts.Msg.RESULT_SUCCESS,
            data: prizeList
        })
    })
}
/**
 * 点击获取奖品
 * @param msg
 * @param session
 * @param next
 */
handler.getPrize = function (msg, session, next) {

    var uid = session.uid;
    if (!uid) {
        next(null, {
            code: consts.Code.ERR,
            msg: consts.Msg.MISSING_PARAMETER
        })
    }
    //获取奖品
    var self = this;
    var prizeId = msg.id;//奖品ID
    lock.acquire(consts.LockKey.WELFARE + uid, function () {
        self.app.rpc.login.loginRemote.getUser(session, uid, function (err, user) {
            if (err) {
                next(null, {
                    code: consts.Code.ERR,
                    msg: err
                })
            }
            //获取奖励信息
            if (!user.welfare) {
                next(null, {
                    code: consts.Code.ERR,
                    msg: "没有抽奖信息"
                })
                return;
            }
            let prizeInfo = user.welfare[prizeId];
            if (!prizeInfo) {
                next(null, {
                    code: consts.Code.ERR,
                    msg: "奖品ID错误"
                })
                return;
            }
            let num = prizeInfo.value;

            //执行方法
            if (prizeInfo.type == consts.MoneyType.OIL) {
                user.welfare_oil = parseInt(user.welfare_oil) + parseInt(num);
                self.app.userRemote(uid, "set", [uid, "user.welfare_oil", user.welfare_oil]);
                //石油
                self.app.rpc.login.loginRemote.addOil(session, uid, session.frontendId, num, consts.OIL_ADD_TYPE.WELFARE, '',function (err, msg) {
                    if (!err) {
                        //推送到客户端
                        //TODO宝箱-1
                        next(null, {
                            code: consts.Code.SUC,
                            msg: consts.Msg.RESULT_SUCCESS
                        })
                    } else {
                        next(null, {
                            code: consts.Code.ERR,
                            msg: msg
                        })
                    }
                })

            } else {
                //金币
                self.app.rpc.login.loginRemote.addCoins(session, uid, session.frontendId, num, true, function () {
                    next(null, {
                        code: consts.Code.SUC,
                        msg: consts.Msg.RESULT_SUCCESS
                    })
                })
                //TODO宝箱-1
            }
            //清空福利
            user.welfare = [];
            self.app.rpc.login.loginRemote.setUser(session, user, function (err, user) {
            });
        })
    })

}
